//@see next js function is for dictionary tree init add edit delete 2008-07-21

/**
* @todo：字典树初始化
* @date：2008-7-31 17:39
* @author：hyh
*/
function ajaxTreeInit(){
	loadTreeParentNode();
	initTree();//初始化树
	initContextMenu();//初始化菜单
}
//读取父节点(固定方法)
function loadTreeParentNode(){
	DWRUtil.useLoadingMessage("正在读取数据...");
	DWREngine.setAsync(false);//设置同步
	imageFolder = "../components/folder-tree-static/images/";
	//父节点
	var html = "<ul id=\"ajax_tree\" class=\"dhtmlgoodies_tree\">";
	ConfigAction.getAlcdictionaryById("0",function(data){
		//配置A，id 提供修改时不用重新读取数据库
		html += "<li><a href=\"javascript:void(0);\" id=\""+  data.id+ "\" onclick=\"getDetailTreeNode('"+ data.id +"');\">"+ data.title +"</a><ul><li parentId=\""+ data.id +"\" class=\"loading.gif\"><a href=\"javascript:void(0);\">Loading...</a></li></ul></li>";
	});
	html += "</ul>";
	$("dic_tree").innerHTML = html;
	DWREngine.setAsync(true);//设置异步
}
//读取子节点(固定方法)
function loadTreeChildNodes(ulId,parentId){
	DWRUtil.useLoadingMessage("正在读取数据...");
	var html = "";
	ConfigAction.listAlcdictionaryByParentId(parentId,function(data){
		if(data != null && data.length >0){
			for(var i = 0;i< data.length;i++){
			html += "<li><a href=\"javascript:void(0);\" id=\""+  data[i].id+ "\" onclick=\"getDetailTreeNode('"+ data[i].id +"');\">"+ data[i].title+ "</a><ul><li parentId=\""+ data[i].id +"\" class=\"loading.gif\"><a href=\"javascript:void(0);\">Loading</a></li></ul></li>";
			}
		}
		$(ulId).innerHTML = html;
		if(html == ""){
			$(ulId).parentNode.getElementsByTagName('IMG')[0].style.visibility = "hidden";
			$(ulId).style.display="none";
		}
		parseSubItems(ulId,parentId);
	});
}

/**
* @todo：添加字典树节点(固定方法)
* @date：2008-7-31 17:37
* @author：hyh
*/
function addTreeNode(pId,pTitle){
	dicOpInit();
	DWRUtil.setValue("parentid",pId);
	DWRUtil.setValue("pTitle",pTitle);
}

/**
* @todo：编辑字典树节点(固定方法)
* @date：2008-7-31 17:37
* @author：hyh
*/
function editTreeNode(id,pTitle){
	dicOpInit(id);
	if(id != "0"){
		DWRUtil.setValue("pTitle",pTitle);
	}else{
		DWRUtil.setValue("pTitle","");
	}
	setDicEditHtml();
}

/**
* @todo：查看树节点详情(固定方法)
* @date：2008-7-31 17:37
* @author：hyh
*/
function getDetailTreeNode(id){
	dicOpInit(id);
	if(id != "0"){
		DWRUtil.setValue("pTitle",$(id).parentNode.parentNode.parentNode.getElementsByTagName('A')[0].innerHTML);
	}else{
		DWRUtil.setValue("pTitle","");
	}
	//setDicDetailHtml();
	setDicEditHtml();
}

/**
* @todo：字典添加编辑详情初始化
* @date：2008-7-31 17:37
* @author：hyh
*/
function dicOpInit(id){
	setDicdetail($("div_dic_op").innerHTML);
	if(id != null && id != ""){//for edit detail
		setDicDetailHtml();//detail
		getDicDetail(id);
	}
}

/**
* @todo：读取字典详情
* @date：2008-7-31 17:37
* @author：hyh
*/
function getDicDetail(id){
	DWRUtil.useLoadingMessage("正在读取数据...");
	ConfigAction.getAlcdictionaryById(id,function(oJSDicBean){
		setFormValue(oJSDicBean);
	});
}

/**
* @todo：字典保存（添加或编辑）
* @date：2008-7-31 17:37
* @author：hyh
*/
function saveDic(){
	DWRUtil.useLoadingMessage("正在保存数据...");
	var oJSDicBean = new JSDicBean();
	getFormValue(oJSDicBean);
	if(oJSDicBean.title == ""){
		alert("字典名称不能为空！");
		$("title").focus();
		return false;
	}
	if(oJSDicBean.keytype == ""){
		alert("字典编码不能为空！");
		$("keytype").focus();
		return false;
	}
	if(oJSDicBean.code == ""){
		alert("字典值不能为空！");
		$("code").focus();
		return false;
	}
	if(!/^[-]{0,1}[0-9]{1,}$/.test(oJSDicBean.code)){
		alert("字典值必须为数字！");
		$("code").focus();
		return false;
	}
	if(oJSDicBean.dicorder == ""){
		alert("字典次序不能为空！");
		$("dicorder").focus();
		return false;
	}
	if(!/^[-]{0,1}[0-9]{1,}$/.test(oJSDicBean.dicorder)){
		alert("字典次序必须为数字！");
		$("dicorder").focus();
		return false;
	}
	if(oJSDicBean.id == ""){
		//新增
		ConfigAction.insertAlcdictionary(oJSDicBean,function(data){
			alert(data);
			setDicdetail("");
			loadNodeByParentId(oJSDicBean.parentid);//重新读取父节点
		});
	}else{
		//编辑
		ConfigAction.updateAlcdictionary(oJSDicBean,function(data){
			loadNodeById(oJSDicBean.id,oJSDicBean.title);//同时修改树的名称
			alert(data);
			setDicDetailHtml();
		});
	}
	//showBeanValue(oJSDicBean);
}

/**
* @todo：删除字典(固定方法)
* @date：2008-7-31 17:37
* @author：hyh
*/
function deleteTreeNode(id,title){
	DWRUtil.useLoadingMessage("正在删除字典数据...");
	ConfigAction.deleteAlcdictionary(id,function(data){
		alert(data);
		setDicdetail("");
	});
}

/**
* @todo：移动树节点
* @date：2008-07-21
* @author：hyh
*/
function moveDicNode(id,pId){
	if(confirm("确定要移动此节点吗？")){
		DWRUtil.useLoadingMessage("正在读取字典数据...");
		ConfigAction.moveAlcdictionaryNode(id,pId,function(data){
			alert(data);
		});
	}else{		
		JSTreeObj.cancelDropDragableNodes();
	}
}

//@see next js function is for dic common function 2008-07-21

/**set dic detail div 2008-07-21*/
function setDicdetail(/*String*/str){
	$("div_dic_detail").innerHTML = str;
}

/**Role Object 2008-07-21*/
function JSDicBean(){
	this.id = "";
	this.keytype = "";
	this.title = "";
	this.code = "";
	this.parentid = "";
	this.dicorder = "";
}

/**展示详情页面 2008-07-21*/
function setDicDetailHtml(){
	$("dicop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">字典信息";
	$("op_button").innerHTML = $("div_hidden_detail").innerHTML;
	setInputReadOnly("title");
	setInputReadOnly("code");
	setInputReadOnly("keytype");
	setInputReadOnly("dicorder");
}

/**展示编辑页面 2008-07-21*/
function setDicEditHtml(){
	$("dicop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">编辑字典";
	$("op_button").innerHTML = $("div_hidden_edit").innerHTML;
  	setInputReadOnly("title","false");
	setInputReadOnly("code","false");
	setInputReadOnly("keytype","false");
	setInputReadOnly("dicorder","false");
}